<!--
 * @Author: xuyijie
 * @Date: 2022-12-06 08:39:46
 * @LastEditors: xuyijie
 * @LastEditTime: 2022-12-07 16:41:43
 * @Description: 文件说明
-->
<script lang="ts" setup>
import { ref } from 'vue'
import { Plus } from '@element-plus/icons-vue'
import useUpload from '@/composables/useUpload'
const { uploadImage } = useUpload()

const props = defineProps<{
  modelValue: string
}>()
const imageUrl = ref(props.modelValue)

const emit = defineEmits<{
  (e: 'update:modelValue', url: string): void
}>()

const request = async (options: any) => {
  const form = new FormData()
  form.append('file', options.file)

  const { data } = await uploadImage(form)
  imageUrl.value = data.url
  emit('update:modelValue', imageUrl.value!)
}
</script>

<template>
  <div class="">
    <el-upload action="" class="avatar-uploader" :http-request="request" :show-file-list="false">
      <img v-if="imageUrl" :src="imageUrl" class="avatar" />
      <el-icon v-else class="avatar-uploader-icon">
        <Plus />
      </el-icon>
    </el-upload>

    <HdError name="file" />
  </div>
</template>

<style scoped lang="scss">
.avatar-uploader {
  @apply text-gray-500;
  :deep(.el-upload) {
    @apply h-28 w-28 border-gray-500 border-dashed border rounded-md duration-200;
    &:hover {
      @apply border-teal-600 border  text-8xl;
      .avatar-uploader-icon {
        @apply text-teal-600;
      }
    }
  }
}
</style>
